﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Steve.TestHelper
{
    public static class ExceptionAssert
    {
        public static void ThrowsException<T>(Action codeThatThrows) where T : Exception
        {
            TestInternal<T>(codeThatThrows, null);
        }

        public static void ThrowsArgumentNullException(Action codeThatThrows, string expectedParamName)
        {
            TestInternal<ArgumentNullException>(codeThatThrows, e => Assert.AreEqual<string>(expectedParamName, e.ParamName));
        }

        private static void TestInternal<T>(Action codeThatThrows, Action<T> additionalAsserts) where T : Exception
        {
            try
            {
                codeThatThrows();
            }
            catch (T e)
            {
                if(additionalAsserts != null) additionalAsserts(e);
                return;
            }

            Assert.Fail("Exception of type '{0}' was not thrown", typeof(T));
        }
    }
}
